<?php
/**
 * 通用控制器
 */
namespace App\Http\Controllers;

use GuzzleHttp\Client;
use Illuminate\Http\Request;

class CommonController extends Controller
{
	/**
	 * 返回随机字符串
	 * @author lai
	 * @date 2017-07-16 14:16:09
	 */
	function randomChar($length = 8){
		$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
		$result = '';

		for ($i = 0; $i<$length; $i++){
			$result .= $chars[mt_rand(0, strlen($chars) - 1)];
		}
		return $result;
	}

	/**
	 * 返回随机字符串
	 * @author lai
	 * @date 2017-07-16 14:16:09
	 */
	function randomNumber($length = 8){
		$chars = '0123456';
		$result = '';

		for ($i = 0; $i<$length; $i++){
			$result .= $chars[mt_rand(0, strlen($chars) - 1)];
		}
		return $result;
	}

	/**
	 * 获取文件的扩展名
	 * @param $target_file 目标文件
	 */
	function getExtends($target_file){
		$file = fopen($target_file, "rb");
		$bin = fread($file, 2); //only read 2 bit
		fclose($file);
		$strInfo = @unpack("C2chars", $bin);
		$typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
		$fileType = '';
		switch ($typeCode)
		{
			case 255216:
				$fileType = 'jpg';
				break;
			case 7173:
				$fileType = 'gif';
				break;
			case 6677:
				$fileType = 'bmp';
				break;
			case 13780:
				$fileType = 'png';
				break;
			default:
				$fileType = 'unknown: '.$typeCode;
		}

		return $fileType;
	}

	/**
	 * @desc curl的get方式，普通http
	 * @param $url 地址
	 * @param $post_data 提交的的数据，array()
	 * @return 返回json
	 */
	public function curl_post_http($url,$postData) {
        $flag = 0;
        $params = '';

        foreach ($postData as $key=>$value) {
            if ($flag!=0) {
                $params .= "&";
                $flag = 1;
            }
            $params.= $key."="; $params.= urlencode($value);// urlencode($value);
            $flag = 1;
        }

        $url .= '?' . $params;

        $con= substr( file_get_contents($url), 0, 1 );  //获取信息发送后的状态

		return $con;
	}

	/**
	 * curl post SSL
	 * @param array $postData 提交的数据
	 * @param string $url
	 * @date 2016-12-12 15:06:12
	 */
	public function curl_post_ssl($url, $vars = '', $second=30,$aHeader=array()){
		$ch = curl_init();
		//超时时间
		curl_setopt($ch,CURLOPT_TIMEOUT,$second);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		//这里设置代理，如果有的话
		//curl_setopt($ch,CURLOPT_PROXY, '10.206.30.98');
		//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);

		//以下两种方式需选择一种

		//第一种方法，cert 与 key 分别属于两个.pem文件
		//默认格式为PEM，可以注释
		//curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		//curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/cert.pem');
		//默认格式为PEM，可以注释
		//curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		//curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/private.pem');

		//第二种方式，两个文件合成一个.pem文件
		// curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/all.pem');
		curl_setopt($ch,CURLOPT_SSLCERT, getcwd(). '/apiclient_cert.pem');
		curl_setopt($ch,CURLOPT_SSLKEY, getcwd(). '/apiclient_key.pem');
		curl_setopt($ch,CURLOPT_CAINFO, getcwd(). '/rootca.pem');

		if( count($aHeader) >= 1 ){
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}

		curl_setopt($ch,CURLOPT_POST, 1);
		if($vars){
			curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
		}

		$data = curl_exec($ch);
		if($data){
			curl_close($ch);
			return $data;
		}
		else {
			$error = curl_errno($ch);
			echo "call faild, errorCode:$error\n";
			curl_close($ch);

			return false;
		}
	}

	/**
	 * @desc curl的get方式，普通http
	 * @param $url 地址
	 * @return 返回json
	 */
	public function curl_get_http($url){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HEADER, 0);

		$out = curl_exec($ch);
		return $out;
	}
}
